package com.ruoyi.event.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.model.UserDrugInfoWrapper;
import com.ruoyi.common.core.domain.vo.UserInfoVo;
import com.ruoyi.common.core.service.UserEventCommonService;
import com.ruoyi.event.domain.*;
import com.ruoyi.event.mapper.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2023/4/5 23:18
 */
@Component
@AllArgsConstructor
public class EventCommonServiceImpl implements UserEventCommonService {

    private final EventBfPlanMapper eventBfPlanMapper;
    private final EventBfRecordMapper eventBfRecordMapper;
    private final EventBfResultMapper eventBfResultMapper;
    private final EventChangeTypeMapper eventChangeTypeMapper;
    private final EventDzbgMapper eventDzbgMapper;
    private final EventGkcsMapper eventGkcsMapper;
    private final EventJcjhMapper eventJcjhMapper;
    private final EventJfMapper eventJfMapper;
    private final EventLeaveMapper eventLeaveMapper;
    private final EventNdkcMapper eventNdkcMapper;
    private final EventPosMapper eventPosMapper;
    private final EventQdMapper eventQdMapper;
    private final EventThMapper eventThMapper;
    private final EventWfxyMapper eventWfxyMapper;
    private final EventWgbgMapper eventWgbgMapper;
    private final EventYwwczlMapper eventYwwczlMapper;
    private final EventYzwfxyMapper eventYzwfxyMapper;
    private final EventZqMapper eventZqMapper;

    @Override
    public void renderUserDrugInfoWrapper(UserDrugInfoWrapper wrapper) {
        EventTh eventTh = eventThMapper.selectOne(Wrappers.<EventTh>lambdaQuery().eq(EventTh::getUserId, wrapper.getUserId()).orderByDesc(EventTh::getThrq).last(" limit 1"));
        if (eventTh != null) {
            wrapper.setThDate(eventTh.getThrq());
        }
        EventQd eventQd = eventQdMapper.selectOne(Wrappers.<EventQd>lambdaQuery().eq(EventQd::getUserId, wrapper.getUserId()).orderByDesc(EventQd::getCreateTime).last(" limit 1"));
        if (eventQd != null) {
            wrapper.setQdDate(eventQd.getCreateTime());
        }
        EventJcjh eventJcjh = eventJcjhMapper.selectOne(Wrappers.<EventJcjh>lambdaQuery().eq(EventJcjh::getUserId, wrapper.getUserId()).eq(EventJcjh::getJcfs, "0").orderByDesc(EventJcjh::getJcrq).last(" limit 1"));
        if (eventJcjh != null) {
            wrapper.setNjDate(eventJcjh.getJcrq());
        }
    }

    @Override
    public void renderUserJf(List<UserInfoVo> userInfoVoList) {
        List<Long> userIdList = userInfoVoList.stream().map(UserInfoVo::getId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(userIdList)) {
            List<EventJf> jfs = eventJfMapper.selectList(Wrappers.<EventJf>lambdaQuery().in(EventJf::getUserId, userIdList));
            Map<Long, EventJf> jfMap = Maps.newHashMap();
            jfs.forEach(jf -> {
                if (!jfMap.containsKey(jf.getUserId())) {
                    jfMap.put(jf.getUserId(), jf);
                } else {
                    EventJf inMap = jfMap.get(jf.getUserId());
                    if (inMap.getJfrq().before(jf.getJfrq())) {
                        jfMap.put(jf.getUserId(), jf);
                    }
                }
            });
            userInfoVoList.forEach(userInfoVo -> {
                if (jfMap.containsKey(userInfoVo.getId())) {
                    userInfoVo.setJfrq(jfMap.get(userInfoVo.getId()).getJfrq());
                }
            });
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clearUserEventInfo(Long userId) {
        eventBfPlanMapper.delete(Wrappers.<EventBfPlan>lambdaQuery().eq(EventBfPlan::getUserId, userId));
        eventBfRecordMapper.delete(Wrappers.<EventBfRecord>lambdaQuery().eq(EventBfRecord::getUserId, userId));
        eventBfResultMapper.delete(Wrappers.<EventBfResult>lambdaQuery().eq(EventBfResult::getUserId, userId));
        eventChangeTypeMapper.delete(Wrappers.<EventChangeType>lambdaQuery().eq(EventChangeType::getUserId, userId));
        eventDzbgMapper.delete(Wrappers.<EventDzbg>lambdaQuery().eq(EventDzbg::getUserId, userId));
        eventGkcsMapper.delete(Wrappers.<EventGkcs>lambdaQuery().eq(EventGkcs::getUserId, userId));
        eventJcjhMapper.delete(Wrappers.<EventJcjh>lambdaQuery().eq(EventJcjh::getUserId, userId));
        eventJfMapper.delete(Wrappers.<EventJf>lambdaQuery().eq(EventJf::getUserId, userId));
        eventLeaveMapper.delete(Wrappers.<EventLeave>lambdaQuery().eq(EventLeave::getUserId, userId));
        eventNdkcMapper.delete(Wrappers.<EventNdkc>lambdaQuery().eq(EventNdkc::getUserId, userId));
        eventPosMapper.delete(Wrappers.<EventPos>lambdaQuery().eq(EventPos::getUserId, userId));
        eventQdMapper.delete(Wrappers.<EventQd>lambdaQuery().eq(EventQd::getUserId, userId));
        eventThMapper.delete(Wrappers.<EventTh>lambdaQuery().eq(EventTh::getUserId, userId));
        eventWfxyMapper.delete(Wrappers.<EventWfxy>lambdaQuery().eq(EventWfxy::getUserId, userId));
        eventWgbgMapper.delete(Wrappers.<EventWgbg>lambdaQuery().eq(EventWgbg::getUserId, userId));
        eventYwwczlMapper.delete(Wrappers.<EventYwwczl>lambdaQuery().eq(EventYwwczl::getUserId, userId));
        eventYzwfxyMapper.delete(Wrappers.<EventYzwfxy>lambdaQuery().eq(EventYzwfxy::getUserId, userId));
        eventZqMapper.delete(Wrappers.<EventZq>lambdaQuery().eq(EventZq::getUserId, userId));
    }
}
